﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace 共享单车管理系统.DDA
{
    public partial class uptaer_1 : Form
    {
        public uptaer_1()
        {
            InitializeComponent();
        }
         SqlDataAdapter daDepartment;
        SqlDataAdapter daLog;
        DataSet ds = new DataSet();
        void init()//初始化
        {
            DB.GetCn();
            string str = "select * from bm";
            string sdr = "select * from log";
            daDepartment = new SqlDataAdapter(str, DB.cn);
            daLog = new SqlDataAdapter(sdr, DB.cn);
            daDepartment.Fill(ds, "department_info");
            daLog.Fill(ds, "log_info");
            DB.cn.Close();
        }

        void showAll()
        {
            DataView dvDepartment = new DataView(ds.Tables["department_info"]);
            dgvDepartment.DataSource = dvDepartment;
            dgvDepartment.Columns[3].Width = 500;
        }
        void dgvHead()
        {
            this.dgvDepartment.Columns[0].HeaderText = "部门编号";
            this.dgvDepartment.Columns[1].HeaderText = "部门名称";
            this.dgvDepartment.Columns[2].HeaderText = "部门经理";
            this.dgvDepartment.Columns[3].HeaderText = "部门描述";
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void uptaer_1_Load(object sender, EventArgs e)
        {
            init();
            showAll();
            dgvHead();
        }

        private void buttonok_Click(object sender, EventArgs e)
        {
            if (textbmjl.Text == "" || txetname.Text == "")
            {
                MessageBox.Show("部门编号和部门名称不能为空");
            }
            else
            {
                DB.GetCn();
                DialogResult dr = MessageBox.Show("确认修改？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    int a = dgvDepartment.CurrentRow.Index;
                    string str = dgvDepartment.Rows[a].Cells["DepartmentID"].Value.ToString();
                    DataRow[] Deprow = ds.Tables["department_info"].Select("DepartmentID='" + str + "'");
                          
                    Deprow[0]["bmname"] = txetname.Text.Trim();//这里报错了索引超出了数组界限
                    Deprow[0]["bujl"] = textbmjl.Text.Trim();
                    Deprow[0]["bmms"] = txtbmms.Text.Trim();
                    MessageBox.Show("修改成功");
                    
                }
                else
                {
                    return;
                }
                SqlCommand cmd = new SqlCommand("add_log", DB.cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("username", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("Time", SqlDbType.DateTime));
                cmd.Parameters.Add(new SqlParameter("table", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("Type", SqlDbType.NVarChar)); 
                cmd.Parameters["username"].Value = Login.UserID;
                cmd.Parameters["Time"].Value = DateTime.Now;
                cmd.Parameters["table"].Value = "bm表";
                cmd.Parameters["Type"].Value = "修改"; 

                try
                {
                    SqlCommandBuilder dbEmployee = new SqlCommandBuilder(daDepartment);
                    daDepartment.Update(ds, "department_info");//这里报错对于不返回任何键列信息的 SelectCommand，不支持 UpdateCommand 的动态 SQL 生成
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                DB.cn.Close();

            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtid.Text = dgvDepartment.CurrentRow.Cells["DepartmentID"].Value.ToString();
            txetname.Text = dgvDepartment.CurrentRow.Cells["bmname"].Value.ToString();
            textbmjl.Text = dgvDepartment.CurrentRow.Cells["bujl"].Value.ToString();
            txtbmms.Text = dgvDepartment.CurrentRow.Cells["bmms"].Value.ToString();
        }

        private void dgvDepartment_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtid.Text = dgvDepartment.CurrentRow.Cells["DepartmentID"].Value.ToString();
            txetname.Text = dgvDepartment.CurrentRow.Cells["bmname"].Value.ToString();
            textbmjl.Text = dgvDepartment.CurrentRow.Cells["bujl"].Value.ToString();
            txtbmms.Text = dgvDepartment.CurrentRow.Cells["bmms"].Value.ToString();
        }
    }
}
